<template>
  <div>
    <el-card>
      <el-row class="header"
              :gutter="20">
        <el-col :span="16">
          <div class="grid-content bg-purple">
            <span>
              行业名称
            </span>
            <el-input v-model="firmName"
                      size="small"
                      placeholder="请输入内容"></el-input>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content bg-purple">
            <el-button size="small"
                       @click="inquire">查询</el-button>
            <el-button size="small"
                       @click="reset">重置</el-button>
          </div>
        </el-col>
      </el-row>
      <el-row :gutter="20"
              class="trade">
        <el-col :span="17">
          <div class="grid-content bg-purple">
            <span>行业数量：{{tableData.length}}</span>
          </div>
        </el-col>
        <el-col :span="7">
          <div class="grid-content bg-purple">
            <el-button size="small"
                       @click="addDialogVisible = true">新增行业</el-button>
          </div>
        </el-col>
      </el-row>
      <div>
        <el-table :data="tableData"
                  border
                  style="width: 100%">
          <el-table-column prop="name"
                           label="行业名称">
          </el-table-column>
          <el-table-column prop="sort"
                           label="排序">
          </el-table-column>
          <el-table-column label="操作">
            <template v-slot="scope">
              <el-button size="small"
                         type="primary"
                         icon="el-icon-edit"
                         circle
                         @click="editIndustry(scope.row)"></el-button>
              <el-button size="small"
                         type="danger"
                         icon="el-icon-delete"
                         circle
                         @click="delIndustry(scope.row)"></el-button>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </el-card>
    <!-- 新增行业弹出层 -->
    <el-dialog title="新增行业"
               :visible.sync="addDialogVisible"
               width="30%">
      <el-form label-width="80px">
        <el-form-item label="行业名称">
          <el-input v-model="industrys.name"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input placeholder="输入数字"
                    type='number'
                    v-model="industrys.sort"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="addDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="submitAdd">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑行业弹出层 -->
    <el-dialog title="新增行业"
               :visible.sync="editDialogVisible"
               width="30%">
      <el-form label-width="80px">
        <el-form-item label="行业名称">
          <el-input v-model="editList.name"></el-input>
        </el-form-item>
        <el-form-item label="排序">
          <el-input placeholder="输入数字"
                    type='number'
                    v-model="editList.sort"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="editDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import '@/mock/shop-industry'
import { setItem, getItem } from '@/utils/storage'
export default {
  name: '',

  components: {},

  props: {},

  data () {
    return {
      tableData: [],
      dialogVisible: false,
      industrys:
      {
        name: '',
        sort: null
      },
      editList:
      {
        name: '',
        sort: null
      },
      firmName: '',
      addDialogVisible: false,
      editDialogVisible: false
    }
  },
  created () {
    this.getIndustry()
  },
  mounted () {

  },
  methods: {
    async getIndustry () {
      if (getItem('industry')) {
        this.tableData = getItem('industry')
      } else {
        const { data } = await this.$http.get('/industry')
        this.tableData = data.list
        setItem('industry', this.tableData)
      }
    },
    submitAdd () {
      if (this.industrys.name.trim() === '') {
        return this.$message.error('行业名称不能为空')
      }
      this.tableData.unshift(this.industrys)
      setItem('industry', this.tableData)
      this.getIndustry()
      this.addDialogVisible = false
    },
    delIndustry (row) {
      this.$confirm('此操作将永久删除该行业, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const arr = []
        this.tableData.forEach(item => {
          if (item !== row) {
            arr.push(item)
          }
        })
        this.tableData = arr
        setItem('industry', this.tableData)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    editIndustry (row) {
      this.editList = row
      this.editDialogVisible = true
    },
    inquire () {
      const arr = []
      if (this.firmName.trim() !== '') {
        this.tableData.forEach(item => {
          if (item.name.indexOf(this.firmName) !== -1) {
            return arr.push(item)
          }
        })
        this.tableData = arr
      }
    },
    reset () {
      this.firmName = ''
      this.getIndustry()
    }
  }
}
</script>

<style scoped lang="less">
.header {
  border: 1px solid #d3d3d3;
  padding: 10px;
  border-radius: 10px;
  .el-input {
    width: 150px;
  }
}
.trade {
  margin: 15px 0;
}
</style>
